﻿using System;
using System.Collections.Generic;
using cfg.level;
using UnityEngine;

namespace QFramework.Monk
{
    public class AbilityBase:ViewController
    {
        protected float mCurTime = 1.5f;//计算cd时间
        
        protected float mSpeed = 1; // 速度，默认为1
        protected int mCount = 3;//生成的数量
        protected float mRadius = 5;//搜索半径
        protected bool mIsSuper = false;//是否为超级技能
        
        protected float mHitBackFactor = 7f;// 击退的力
        
        protected int mLevel = -1;//-1未解锁
        
        public int Level
        {
            get => mLevel;
            private set => mLevel = value;
        }

        public float Speed
        {
            get => mSpeed;
            private set => mSpeed = value;
        }
        
        public float Radius
        {
            get => mRadius;
            private set => mRadius = value;
        }
        
        public float CurTime
        {
            get => mCurTime;
            private set => mCurTime = value;
        }
        
        public bool IsSuper
        {
            get => mIsSuper;
            private set => mIsSuper = value;
        }
        
        public float HitBackFactor
        {
            get => mHitBackFactor;
            private set => mHitBackFactor = value;
        }
        
        
        public virtual void LevelUp(SkillItem config)
        {
            mLevel++;
            switch (config.ChangeValueType)
            {
                case ChangeValueType.Unlock:
                    UnLockAbility();
                    break;
                case ChangeValueType.Count:
                    mCount += config.Value;
                    OnCountAdd();
                    break;
                case ChangeValueType.Radius:
                    mRadius += config.Value;
                    break;
                case ChangeValueType.Special:
                    mIsSuper = true;
                    OnSkillChange();
                    break;
                case ChangeValueType.Speed:
                    mSpeed += config.Value;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
        
        protected virtual void OnCountAdd()
        {
            
        }

        protected virtual void OnSkillChange()
        {
           
        }

        private void Awake()
        {
            this.Hide();
        }

        public virtual void UnLockAbility(bool isForce = false)
        {
            if (isForce)
            {
                mLevel++;
            }
            if (mLevel>=0)
            {
                this.Show();
            }
        }

        public virtual void InitAbility()
        {
        }

        //查找离玩家最近几个的敌人
        protected List<EnemyBase> FindNearestEnemies()
        {
            Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, mRadius, 1 << LayerMask.NameToLayer("Enemy"));
            List<EnemyBase> nearestEnemies = new List<EnemyBase>(mCount);

            // 使用一个列表来存储距离和对应的敌人
            List<(float Distance, EnemyBase Enemy)> distances = new List<(float, EnemyBase)>();

            foreach (Collider2D collider in colliders)
            {
                EnemyBase enemy = collider.GetComponentInParent<EnemyBase>();
                if (enemy != null)
                {
                    float squaredDistance = (enemy.transform.position - transform.position).sqrMagnitude;
                    distances.Add((squaredDistance, enemy));
                }
            }

            // 对距离进行排序
            distances.Sort((a, b) => a.Distance.CompareTo(b.Distance));

            // 取前几个最近的敌人
            for (int i = 0; i < Mathf.Min(mCount, distances.Count); i++)
            {
                nearestEnemies.Add(distances[i].Enemy);
            }

            return nearestEnemies;
        }
    }
}